package com.bawei.park2302.visit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.utils.utils.entity.VisitBeanItem
import com.bawei.lib_base.utils.utils.entity.VisitStatusBean
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.park2302.R
import com.bawei.park2302.adapter.VisitAdapter
import com.bawei.park2302.adapter.VisitStatusAdapter
import com.bawei.park2302.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding,VisitIntent,VisitViewModel>() {
    override val binding: ActivityVisitBinding by lazy {
        ActivityVisitBinding.inflate(layoutInflater)
    }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()

    val visitAdapter by lazy { VisitAdapter() }
    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    var data = 0
    var status = "全部"

    override fun initView() {
        val arrays = mutableListOf<VisitStatusBean>(
            VisitStatusBean("全部状态",true),VisitStatusBean("待审核",false),
            VisitStatusBean("已通过",false),VisitStatusBean("已完成",false),
            VisitStatusBean("驳回",false)
        )
        visitStatusAdapter.addData(arrays)

        binding.rvVisitStatus.apply {
            adapter = visitStatusAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity,LinearLayoutManager.HORIZONTAL,false)
        }

        binding.rvVisit.apply {
            adapter = visitAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity)
        }

        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/VisitDetailActivity")
                .withInt("visitId",visitAdapter.data[position].id).navigation()
        }

        binding.ivAdd.setOnClickListener {
            ARouter.getInstance().build("/app/VisitAddActivity").navigation()
        }

        binding.visitSv.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                vm.sendIntent(VisitIntent.visitName(query!!))
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()){
                    vm.handelIntent(VisitIntent.visit(data))
                }
                return true
            }
        })

        visitStatusAdapter.setOnItemClickListener{ adapter, view, position ->
            visitStatusAdapter.data.forEach {
                it.isChecked = false
            }
            visitStatusAdapter.data[position].isChecked = true
            visitStatusAdapter.notifyDataSetChanged()

            status = visitStatusAdapter.data[position].status
            if (position == 0){
                vm.handelIntent(VisitIntent.visit(data))
            }else{
                vm.handelIntent(VisitIntent.visitStatus(visitStatusAdapter.data[position].status))
            }
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        visitAdapter.data.clear()
                        val beanItems = it.data as Collection<VisitBeanItem>
                        visitAdapter.addData(beanItems)
                        visitAdapter.notifyDataSetChanged()
                    }
                    else -> {}
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.handelIntent(VisitIntent.visit(data))
    }
}